/*==============================================================================
            Copyright (c) 2010-2011 QUALCOMM Incorporated.
            All Rights Reserved.
            Qualcomm Confidential and Proprietary
==============================================================================*/

package com.AndroidUI;

import com.AR_Edificacio.R;
import com.Domain.Channel;
import com.EdificacioAR.AugmentedRealityActivity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class GUIOverlayManager {

    // Custom views
    private View overlayView;
    private Button nextButton;
    private Button previousButton;
    
    // The main application context
    private Context applicationContext;
    
    // A Handler for working with the gui from other threads
    private Handler mainActivityHandler;
    
    // Flags for our Handler
    public static final int PRESSED_NEXT_BUTTON = 0;
    public static final int PRESSED_PREVIOUS_BUTTON = 1;
    
    /** Initialize the ModelSwitcherGUI. */
    public GUIOverlayManager(Context context)
    {
        // Load our overlay view
        // This view will add content on top of the camera / opengl view
        overlayView = View.inflate(context, R.layout.modelswitcher_gui, null);

        // Store the application context
        applicationContext = context;
        
        // This can be used to make changes to the GUI
        // Create a Handler from the current thread
        // This is the only thread that can make changes to the GUI,
        // so we require a handler for other threads to make changes
        /*mainActivityHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                	case PRESSED_NEXT_BUTTON:
                		break;
                	case PRESSED_PREVIOUS_BUTTON:
                		break;
                
                    /*case SHOW_DELETE_BUTTON:
                        if (deleteButton != null) {
                            deleteButton.setVisibility(View.VISIBLE);
                        }
                        break;
                    case HIDE_DELETE_BUTTON:
                        if (deleteButton != null) {
                            deleteButton.setVisibility(View.INVISIBLE);
                        }
                        break;
                    case TOGGLE_START_BUTTON:
                        if (startButton != null) {
                            startButton.setChecked(true);
                        }
                        break;
                    case DISPLAY_INFO_TOAST:
                        String text = (String) msg.obj;
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(applicationContext, text, duration);
                        toast.show();
                        break;
                }
            }
        };*/
    }
    
    
    /** Button clicks should call corresponding native functions. */
    public void initButtons()
    {
        if (overlayView == null)
            return;
        
        nextButton = (Button)overlayView.findViewById(R.id.Next);
        nextButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				nativeNextModel();
			}
		});
        
        previousButton = (Button)overlayView.findViewById(R.id.Previous);
        previousButton.setOnClickListener(new View.OnClickListener() {
			
    		@Override
			public void onClick(View v) {
    			nativePreviousModel();
			}
		});

    }
    
    
    /** Clear the button listeners. */
    public void deinitButtons()
    {
        if (overlayView == null)
            return;
        
        nextButton.setOnClickListener(null);
        previousButton.setOnClickListener(null);
        
        nextButton = null;
        previousButton = null;

    }
    
    
    /** Send a message to our gui thread handler. */
    public void sendThreadSafeGUIMessage(Message message)
    {
        mainActivityHandler.sendMessage(message);
    }
    
    
    /** Getter for the overlay view. */
    public View getOverlayView()
    {
        return overlayView;
    }
    
    // Native functions
    private native void nativeNextModel();
    private native void nativePreviousModel();
}
